package com.gelpag.base.business.common;

import been.UserInfo;
import entity.Const;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    CommonService commonService;

    @PostMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {

        //通过请求获取token
        String token = request.getHeader("token");

        try {
            //通过token获取用户信息
            UserInfo userInfo = CommonBase.getUserInFromToken(token);
            if (userInfo == null) {
                return new Result(Const.RESULT_F1,Const.ERROR_MSG_F1,token,"");
            }

            Map<String, Object> mapUser = new HashMap<>();

            mapUser.put("user_id",userInfo.getUser_id());
            mapUser.put("user_name",userInfo.getUser_name());
            mapUser.put("user_level",userInfo.getUser_level());
            mapUser.put("user_type",userInfo.getUser_type());
//            mapUser.put("user_type_name",userInfo.getuse)
            mapUser.put("unit_id",userInfo.getUnit_ids().size()>0?userInfo.getUnit_ids().get(0):"");

            List<Map<String, String>> userSys_codes = commonService.getUserSys_codes(userInfo.getUser_id());
            mapUser.put("sys_codes", userSys_codes);

            return new Result(Const.RESULT_0,"请求成功",token,mapUser);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Const.RESULT_1, "查询失败！", token,"");
        }
    }
}
